// hooks/useCounter.ts
import { ref, onMounted, watch } from 'vue';

export function useCounter(initialValue: number = 0) {
  const countDown = ref(initialValue);
  let intervalId: NodeJS.Timeout | undefined;

  const startCountDown = (seconds: number = initialValue) => {
    countDown.value = seconds;
    if (intervalId) clearInterval(intervalId);
    intervalId = setInterval(() => {
      if (countDown.value > 0) {
        countDown.value -= 1;
      } else {
        clearInterval(intervalId);
      }
    }, 1000);
  };

  onMounted(() => {
    if (initialValue > 0) {
      startCountDown(initialValue);
    }
  });

  return [countDown, startCountDown] as const;
}